package commons.conf.xml;

import org.hibernate.type.AbstractStandardBasicType;
import org.hibernate.type.BasicTypeRegistry;

import commons.BizException;

/**
 * integer, long, short, float, double, character, byte, boolean
 * <a>
 * http://docs.jboss.org/hibernate/core/3.6/reference/zh-CN/html/mapping.html#mapping-types
 * </a>
 * @author jkyang
 */
public class TypeMapping {

	public static final BasicTypeRegistry type = new BasicTypeRegistry();
	
	@SuppressWarnings("unchecked")
	public static Class getMappingType(String xmlType) {
		AbstractStandardBasicType bt = (AbstractStandardBasicType)type.getRegisteredType(xmlType);
		if(bt == null) {
			throw new BizException(xmlType + " can not found!!!");
		}
		return bt.getReturnedClass();
	}
	
	public static String getMappingTypeString(String xmlType) {
		Class<?> c = getMappingType(xmlType);
		String ret = c.getName();
		if(c == byte[].class) {
			ret = "byte[]";
		}
		return ret ;
	}
	
	
	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		BasicTypeRegistry r = new BasicTypeRegistry();
		AbstractStandardBasicType bt = (AbstractStandardBasicType)r.getRegisteredType("binary");
		System.out.println(bt.getReturnedClass().getName());
	}
}
